package com.sun.syndication.samples;

import java.net.URL;
import java.io.InputStreamReader;
import java.util.*;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
 * It Reads and prints any RSS/Atom feed type.
 * <p>
 * @author Alejandro Abdelnur
 *
 */
public class FeedReader {

    public static void main(String[] args) {
        boolean ok = false;
        if (args.length==1) {
            try {

//                String url = "http://ezrss.it/search/index.php?show_name=Big+Bang&date=&quality=&release_group=&mode=rss";
//                String url = "http://www.mininova.org/rss/House/8";
//                String url = "http://www.mininova.org/rss/Big%20Bang/8";
//                String url = "http://www.torrentz.com/feed?q=big+bang";
//                String url = "http://myepisodes.com/rss.php?feed=mylist&uid=username&pwdmd5=hashcode&onlyunacquired=1";
                String url = "http://www.torrentz.com/feed?q=house";
//                String url = "http://www.torrentz.com/8d66d3d77073db7d983437fad7e2304e09c857f0";

                URL feedUrl = new URL(url);

                SyndFeedInput input = new SyndFeedInput();
                SyndFeed feed = input.build(new XmlReader(feedUrl));

                List entries = feed.getEntries();

//                Calendar cal = Calendar.getInstance(TimeZone.getDefault());
//                cal.add(Calendar.DAY_OF_YEAR,-14);
//                Date start = cal.getTime();
//                System.out.println("start = " + start);

                for (int i = 0; i < entries.size(); i++) {
                    SyndEntryImpl s = (SyndEntryImpl) entries.get(i);
//                    Date date = s.getPublishedDate();
//                    if(date.after(start)) {
//                        System.out.println(date);
//                        System.out.println(s.getTitle());
//                        System.out.println(s.getDescription().getValue());
//                        System.out.println(s.getLink());
//                        System.out.println();
//                    }
                    System.out.println(s);
                    System.out.println("~~~~~~~~~~~~~~~~~");
                }

//                System.out.println(feed);

                ok = true;
            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("ERROR: "+ex.getMessage());
            }
        }

        if (!ok) {
            System.out.println();
            System.out.println("FeedReader reads and prints any RSS/Atom feed type.");
            System.out.println("The first parameter must be the URL of the feed to read.");
            System.out.println();
        }
    }

}



/*

sample urls :


http://www.mininova.org/rss/Californication/8
http://ezrss.it/search/index.php?show_name=Dexter&show_name_exact=true&date=&quality=HDTV&quality_exact=true&release_group=&mode=rss

http://ezrss.it/search/index.php?show_name=Dexter&show_name_exact=true&date=2009-10-26&quality=HDTV&quality_exact=true&release_group=&mode=rss


sample config xml:

<show>
 <name>Californication</name>
 <url>http://ezrss.it/search/index.php?show_name=Californication&show_name_exact=true&date=&quality=HDTV&release_group=&mode=rss</url>
 <filter>eztv</filter>
 <not>promo 720p</not>
</show>



*/